/*+**************************************************************************/
/***                                                                      ***/
/***   This file is distributed under a BSD license.                      ***/
/***   See LICENSE.txt for details.                                       ***/
/***                                                                      ***/
/**************************************************************************+*/

/****************************************************************************/
/***                                                                      ***/
/***   (C) 2005 Dierk Ohlerich, all rights reserved                       ***/
/***                                                                      ***/
/****************************************************************************/

level11;

asc
{
  cbuffer TestMtrlVSPara : register(c0) : slot vs 0
  {
    float4 uvoffset;
  };
  cbuffer TestMtrlGSPara : register(c0) : slot gs 0
  {
    row_major float4x4 mvp;
    float4 ldir;
  };
}


material TestMtrl
{
  // the vertex shader

  vs
  {    
    asc vs_4_0                 // hlsl code
    {
      sampler2D s0 : register(s0);
      use TestMtrlVSPara;

      void main
      (
        in float3 in_pos : POSITION,            // input, from vertex array
        in float3 in_norm : NORMAL,
        in float2 in_uv : TEXCOORD0,
        out float3 out_pos : POSIN,             // position last for DX11
        out float out_d : DIST,
      ) 
      {
        float d = tex2Dlod(s0,float4(in_uv+uvoffset.xy,0,0)).x
                + tex2Dlod(s0,float4(in_uv+uvoffset.zw,0,0)).x;
        out_pos = in_pos+in_norm*(d-1)*0.7;
      }
    }
  }

  // geometry shader

  gs
  {
    asc gs_4_0
    {
      use TestMtrlGSPara;

      struct tvin
      {
        float3 pos : POSIN;
      };
      struct tvout
      {
        float4 col : COLOR0;
        float4 pos : POSITION;
      };

      [maxvertexcount(6)]
      void main
      (
        triangle tvin vin[3],
        inout TriangleStream<tvout> vouts,
      )
      {
        tvout vout;

        float3 d0 = vin[0].pos - vin[1].pos;
        float3 d1 = vin[0].pos - vin[2].pos;
        float3 norm = normalize(cross(d0,d1));

        float l = saturate(-dot(norm,ldir))*0.75+0.25;

        vout.col = float4(l,l,l,1);

        vout.pos = mul(float4(vin[0].pos,1),mvp);
        vouts.Append(vout);
        vout.pos = mul(float4(vin[1].pos,1),mvp);
        vouts.Append(vout);
        vout.pos = mul(float4(vin[2].pos,1),mvp);
        vouts.Append(vout);

        float d = -0.2;
        vouts.RestartStrip();
        vout.pos = mul(float4(vin[0].pos+d*norm,1),mvp);
        vouts.Append(vout);
        vout.pos = mul(float4(vin[1].pos+d*norm,1),mvp);
        vouts.Append(vout);
        vout.pos = mul(float4(vin[2].pos+d*norm,1),mvp);
        vouts.Append(vout);
      }
    }
  }

  // the pixel shader

  ps
  {

    asc ps_4_0
    {
      void main
      (
        in float4 col : COLOR0,
        out float4 result : COLOR0
      )
      {
        result = col;
      }
    }
  }
};

  